package com.suke.money.Web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.money.suke.utils.Constants;
import com.suke.money.model.LoanInfo;
import com.suke.money.service.BidInfoService;
import com.suke.money.service.LoanInfoService;
import com.suke.money.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liush
 * @CreateTime: 2022-11-20  14:49
 * @Description: 盈利宝首页控制层
 */
/*
 * 启动注册中心 zookeeper 服务
 * [root@Suke]# cd /usr/local/apache-zookeeper-3.5.5-bin/bin
 * [root@Suke bin]# ./zkServer.sh start
 * 启动 redis 服务
 * [root@Suke]# cd /usr/local/redis-4.0.6/src
 * [root@Suke src]# ./redis-server ../redis.conf &
 * 查看redis和zookeeper进程
 * ps -ef|grep redis 或 ps aux|grep zookeeper
 * */
@Slf4j
@Controller
public class IndexController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 20000)
    LoanInfoService loanInfoService;

    @Reference(interfaceClass = UserService.class, version = "1.0.0", timeout = 20000)
    UserService userService;

    @Reference(interfaceClass = BidInfoService.class, version = "1.0.0", timeout = 20000)
    BidInfoService bidInfoService;

    //http://localhost:9005/web/index
    @RequestMapping("index")
    public String index(Model model) {
        //1、动力金融网历史年化收益率==》产品平均利率
        Double loanInfoHistoryRateAvg = loanInfoService.queryLoanInfoHistoryRateAvg();
        model.addAttribute(Constants.LOAN_INFO_HISTORY_RATE_AVG, loanInfoHistoryRateAvg);

        //2、平台用户数==》注册人数
        Long userCount = userService.queryUserCount();
        model.addAttribute(Constants.USER_COUNT, userCount);

        //3、累计成交额==》投资总金额
        Double bidInfoMoneySum = bidInfoService.queryBidInfoMoneySum();
        model.addAttribute(Constants.BID_INFO_MONEY_SUM, bidInfoMoneySum);

        /*
         * ptype:数据库对应-类LoanInfo的属性productType(产品类型);
         * start:数据库中从第几个开始查询
         * content:数据库中查询几个数据
         * */
        Map<String, Object> parasMap = new HashMap<>();
        //4、新手宝
        parasMap.put("ptype", 0);
        parasMap.put("start", 0);
        parasMap.put("content", 1);
        List<LoanInfo> loanInfos_X = loanInfoService.queryLoanInfosByTypeAndNums(parasMap);
        model.addAttribute("loanInfos_X", loanInfos_X);
        //5、优选标
        parasMap.put("ptype", 1);
        parasMap.put("start", 0);
        parasMap.put("content", 4);
        List<LoanInfo> loanInfos_Y = loanInfoService.queryLoanInfosByTypeAndNums(parasMap);
        model.addAttribute("loanInfos_Y", loanInfos_Y);
        //6、散标
        parasMap.put("ptype", 2);
        parasMap.put("start", 0);
        parasMap.put("content", 8);
        List<LoanInfo> loanInfos_S = loanInfoService.queryLoanInfosByTypeAndNums(parasMap);
        model.addAttribute("loanInfos_S", loanInfos_S);

        return "index.html";
    }
}
